#include <stdio.h>
#include <pthread.h>
#include <arpa/inet.h>
#include "Tcp_Sock.h"

#define SERVICE_PORT 8080
#define MAX_CLIENTS 50

typedef struct
{
    int socket;
    struct sockaddr_in address;
    int uid;
    int active;
} client_t;

// 初始化互斥锁
pthread_mutex_t clients_mutex = PTHREAD_MUTEX_INITIALIZER;
// 初始用户id
int uid_counter = 0;
// 创建用户列表
client_t *clients[MAX_CLIENTS];

void *client_handler(void *arg);
void add_client(client_t *cli);
void remove_client(int uid);
void View_ClientInfo(int client_fd,client_t *cli);


int main(int argc, char *argv[])
{
    int service_fd = Service_Init(NULL, SERVICE_PORT);
    if (service_fd == -1)
        return -1;
    printf("%s\n", __FUNCTION__);
    // 执行服务器逻辑
    while (1)
    {
        /**
         * 查看是否有用户接入,
         * 如果有，执行下面客户创建;
         * 如果没有，执行continue.
         */
        struct sockaddr_in client_addr;
        socklen_t addr_len = sizeof(client_addr);
        int client_fd = accept(service_fd, (struct sockaddr *)&client_addr, &addr_len);
        if (client_fd < 0)
        {
            perror("接受连接失败");
            continue;
        }
        /**
         * 创建客户端结构
         * 首先创建一个client_t类型的结构体cli
         * 然后cli->socket保存了与客户端通信的文件描述符
         * cli->address则保存了客户端的地址信息
         */
        client_t *cli = (client_t *)malloc(sizeof(client_t));
        cli->socket = client_fd;
        cli->address = client_addr;
        View_ClientInfo(client_fd,cli);

#if 0
        /* 添加到客户端列表 */
        add_client(cli);
        /* 创建线程 */
        pthread_t tid;
        if (pthread_create(&tid, NULL, client_handler, (void *)cli) != 0)
        {
            perror("线程创建失败");
            close(client_fd);
            free(cli);
        }
#endif
    }
    Service_Close(service_fd);
    return 0;
}

/**
 * @brief 查看连接的客户端的基本信息
 * 
 * @param client_fd 
 * @param cli 
 */
void View_ClientInfo(int client_fd,client_t *cli)
{
    printf("client:%d\n",client_fd);
    // 转换IP地址为字符串并打印
    char ip_str[INET_ADDRSTRLEN];
    if (inet_ntop(AF_INET, &(cli->address.sin_addr), ip_str, sizeof(ip_str)) == NULL)
    {
        perror("IP地址转换失败");
        return ;
    }
    else
    {
        printf("客户端IP地址: %s\n", ip_str);
        printf("客户端端口号: %d\n", ntohs(cli->address.sin_port));
    }
}

void *client_handler(void *arg)
{
    client_t *p = (client_t *)(arg);

    free(p);
}

/* 添加新客户端到列表 */
void add_client(client_t *cli)
{
    pthread_mutex_lock(&clients_mutex);
    for (int i = 0; i < MAX_CLIENTS; ++i)
    {
        if (!clients[i])
        {
            cli->uid = uid_counter++;
            cli->active = 1;
            clients[i] = cli;
            break;
        }
    }
    pthread_mutex_unlock(&clients_mutex);
}

/* 移除指定客户端 */
void remove_client(int uid)
{
    pthread_mutex_lock(&clients_mutex);
    for (int i = 0; i < MAX_CLIENTS; ++i)
    {
        if (clients[i] && clients[i]->uid == uid)
        {
            clients[i]->active = 0;
            break;
        }
    }
    pthread_mutex_unlock(&clients_mutex);
}